//
//  BCUserNoSvipContentCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/20.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCUserNoSvipContentCell.h"

//
#import "BCUserSvipDataContent.h"

static CGFloat BCUserNoSvipContentCellTopBottomPadding = 20.0;
static CGFloat BCUserNoSvipContentCellLeftRightPadding = 20.0;
//
static CGFloat BCUserNoSvipContentCellIconContentPadding = 8.0;

@interface BCUserNoSvipContentCell ()

@property (nonatomic, strong) UIImageView *iconIV;

/**
 *contentLabel
 */
@property (nonatomic, strong) QMUILabel *contentLabel;

@end

@implementation BCUserNoSvipContentCell

- (CGSize)iconSize {
    return CGSizeMake(26.0, 26.0);
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.iconIV = [UIImageView new];
    self.iconIV.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:self.iconIV];
    //
    self.contentLabel = [QMUILabel new];
    self.contentLabel.font = [UIFont systemFontOfSize:19 weight:UIFontWeightRegular];
    self.contentLabel.textColor = [UIColor darkMainColor];
    self.contentLabel.numberOfLines = 0;
    [self.contentView addSubview:self.contentLabel];
    
    //
    CGSize iconSize = [self iconSize];
    CGFloat leftPadding = BCUserNoSvipContentCellLeftRightPadding+iconSize.width+BCUserNoSvipContentCellIconContentPadding;
    //
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(BCUserNoSvipContentCellTopBottomPadding);
        make.left.mas_equalTo(self.contentView).offset(leftPadding);
        make.right.mas_equalTo(self.contentView).offset(-BCUserNoSvipContentCellLeftRightPadding);
    }];
    //
    [self.iconIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(20.0);
        make.size.mas_equalTo(iconSize);
        make.top.mas_equalTo(self.contentLabel).offset(self.contentLabel.font.lineHeight/2-iconSize.height/2);
    }];
}

- (CGSize)sizeThatFits:(CGSize)size {
    CGFloat height = 0;
    height += BCUserNoSvipContentCellTopBottomPadding;
    //
    CGSize iconSize = [self iconSize];
    CGFloat leftPadding = BCUserNoSvipContentCellLeftRightPadding+iconSize.width+BCUserNoSvipContentCellIconContentPadding;
    CGFloat contentWidth = size.width-leftPadding-BCUserNoSvipContentCellLeftRightPadding;
    CGSize contentSize = CGSizeMake(contentWidth, CGFLOAT_MAX);
    //
    contentSize = [self.contentLabel sizeThatFits:contentSize];
    height += contentSize.height;
    //
    height += BCUserNoSvipContentCellTopBottomPadding;
    return CGSizeMake(size.width, height);
}


- (void)setContent:(BCUserSvipDataContentModel *)content {
    _content = content;
    //
    [self.iconIV sd_setImageWithURL:[NSURL URLWithString:content.icon]];
    //
    NSString *title = content.title?:@"";
    NSString *contents = content.content?:@"";
    NSString *contentString = [NSString stringWithFormat:@"%@\n%@",title,contents];
    NSMutableAttributedString *contentAttributedText = [[NSMutableAttributedString alloc] initWithString:contentString];
    NSRange contentRange = [contentString rangeOfString:contents];
    if (contentRange.location!=NSNotFound) {
        [contentAttributedText addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular],NSForegroundColorAttributeName:[UIColor qmui_colorWithHexString:@"#505050"]} range:contentRange];
    }
    self.contentLabel.attributedText = contentAttributedText;
}

@end
